Skip to contentSkip to conversation

Change in openvpn[master]: init: Fix conversion warnings

Openvpn-devel flichtenheld (Code Review) 1 participant 5 messages Oct 8, 2025 – Mar 13 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1759939917000.I41b9f7686180f2549bd4984cbbf66059f0ba2b15@...2715...

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email

to review the following change.

Change subject: init: Fix conversion warnings
......................................................................

init: Fix conversion warnings

Can be separated in mostly two classes of issues:
- Return type of get_random is long, but we ensure that value is in range so we can cast it to whatever we need.
- Doing MTU match in size_t even though most of the input values are int.

Change-Id I41b9f7686180f2549bd4984cbbf66059f0ba2b15

---
M src/openvpn/init.c

1 file changed, 14 insertions(+), 26 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/59/1259/1

src/openvpn/init.c +14 -26
index f8a0fee..81b9ca9 100644
@@ -455,11 +455,6 @@
 }
 #endif /* ENABLE_MANAGEMENT */
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
 /*
  * Initialize and possibly randomize the connection list.
  *
@@ -482,7 +477,7 @@
         int i;
         for (i = l->len - 1; i > 0; --i)
         {
-            const int j = get_random() % (i + 1);
+            const long j = get_random() % (i + 1);
             if (i != j)
             {
                 struct connection_entry *tmp;
@@ -842,7 +837,7 @@
     struct timeval tv;
     if (!gettimeofday(&tv, NULL))
     {
-        const unsigned int seed = (unsigned int)tv.tv_sec ^ tv.tv_usec;
+        const unsigned int seed = (unsigned int)(tv.tv_sec ^ tv.tv_usec);
         srandom(seed);
     }
 
@@ -2873,17 +2868,17 @@
     }
 }
 
-static size_t
+static int
 get_frame_mtu(struct context *c, const struct options *o)
 {
-    size_t mtu;
+    int mtu;
 
     if (o->ce.link_mtu_defined)
     {
         ASSERT(o->ce.link_mtu_defined);
         /* if we have a link mtu defined we calculate what the old code
          * would have come up with as tun-mtu */
-        size_t overhead = frame_calculate_protocol_header_size(&c->c1.ks.key_type, o, true);
+        int overhead = (int)frame_calculate_protocol_header_size(&c->c1.ks.key_type, o, true);
         mtu = o->ce.link_mtu - overhead;
     }
     else
@@ -2894,7 +2889,7 @@
 
     if (mtu < TUN_MTU_MIN)
     {
-        msg(M_WARN, "TUN MTU value (%zu) must be at least %d", mtu, TUN_MTU_MIN);
+        msg(M_WARN, "TUN MTU value (%d) must be at least %d", mtu, TUN_MTU_MIN);
         frame_print(&c->c2.frame, M_FATAL, "MTU is too small");
     }
     return mtu;
@@ -2923,7 +2918,7 @@
      * space to allow server to push "baby giant" MTU sizes */
     frame->tun_max_mtu = max_int(TUN_MTU_MAX_MIN, frame->tun_max_mtu);
 
-    size_t payload_size = frame->tun_max_mtu;
+    int payload_size = frame->tun_max_mtu;
 
     /* we need to be also large enough to hold larger control channel packets
      * if configured */
@@ -2942,7 +2937,7 @@
 
     /* the space that is reserved before the payload to add extra headers to it
      * we always reserve the space for the worst case */
-    size_t headroom = 0;
+    int headroom = 0;
 
     /* includes IV and packet ID */
     headroom += crypto_max_overhead();
@@ -2966,11 +2961,11 @@
     /* the space after the payload, this needs some extra buffer space for
      * encryption so headroom is probably too much but we do not really care
      * the few extra bytes */
-    size_t tailroom = headroom;
+    int tailroom = headroom;
 
 #ifdef USE_COMP
     msg(D_MTU_DEBUG,
-        "MTU: adding %zu buffer tailroom for compression for %zu "
+        "MTU: adding %d buffer tailroom for compression for %d "
         "bytes of payload",
         COMP_EXTRA_BUFFER(payload_size), payload_size);
     tailroom += COMP_EXTRA_BUFFER(payload_size);
@@ -3289,18 +3284,15 @@
     if (options->renegotiate_seconds_min < 0)
     {
         /* Add 10% jitter to reneg-sec by default (server side only) */
-        int auto_jitter = options->mode != MODE_SERVER
-                              ? 0
-                              : get_random() % max_int(options->renegotiate_seconds / 10, 1);
+        int jitter_max = max_int(options->renegotiate_seconds / 10, 1);
+        int auto_jitter = options->mode != MODE_SERVER ? 0 : (int)(get_random() % jitter_max);
         to.renegotiate_seconds = options->renegotiate_seconds - auto_jitter;
     }
     else
     {
         /* Add user-specified jitter to reneg-sec */
-        to.renegotiate_seconds =
-            options->renegotiate_seconds
-            - (get_random()
-               % max_int(options->renegotiate_seconds - options->renegotiate_seconds_min, 1));
+        int jitter_max = max_int(options->renegotiate_seconds - options->renegotiate_seconds_min, 1);
+        to.renegotiate_seconds = options->renegotiate_seconds - (int)(get_random() % jitter_max);
     }
     to.single_session = options->single_session;
     to.mode = options->mode;
@@ -3495,10 +3487,6 @@
     }
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
 /*
  * No encryption or authentication.
  */

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I41b9f7686180f2549bd4984cbbf66059f0ba2b15
Gerrit-Change-Number: 1259
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email )

Change subject: init: Fix conversion warnings
......................................................................

Patch Set 1: Code-Review-1

(1 comment)

Patchset:

PS1:
I am not happy with this - there's a number of clear "positive integer values only" variables involved that now change to "int" (like headroom, tailroom). I can see that it's tricky (because ce.*mtu are all "int" as well and that change would affect more places). Not sure whether I have a useful suggestion right now.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I41b9f7686180f2549bd4984cbbf66059f0ba2b15
Gerrit-Change-Number: 1259
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Fri, 10 Oct 2025 11:04:33 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: cron2, plaisthos.

flichtenheld has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email )

Change subject: init: Fix conversion warnings
......................................................................

Patch Set 6:

(1 comment)

Patchset:

PS1:

Quoted 1 line

I am not happy with this - there's a number of clear "positive integer values only" variables involv […]

I have rechecked my changes. I really think this is the best possible code without changing the types of struct frame and struct buffer. Both of which would have widespread repercussions (obviously more for buffer than frame). So I would recommend we go with this for now.

Signature · 19 lines
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I41b9f7686180f2549bd4984cbbf66059f0ba2b15
Gerrit-Change-Number: 1259
Gerrit-PatchSet: 6
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Comment-Date: Mon, 27 Oct 2025 11:52:47 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: cron2 <[email protected]...>
#4 lore raw
In reply to a message not in the archive

Attention is currently required from: cron2, plaisthos.

Hello cron2, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email

to look at the new patch set (#15).

The following approvals got outdated and were removed:
Code-Review-1 by cron2

Change subject: init: Fix conversion warnings
......................................................................

init: Fix conversion warnings

Can be separated in mostly two classes of issues:
- Return type of get_random is long, but we ensure that value is in range so we can cast it to whatever we need.
- Doing MTU match in size_t even though most of the input values are int.

Change-Id I41b9f7686180f2549bd4984cbbf66059f0ba2b15

---
M src/openvpn/init.c

1 file changed, 17 insertions(+), 25 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/59/1259/15

src/openvpn/init.c +17 -25
index 66a5836..a7c13f9 100644
@@ -454,11 +454,6 @@
 }
 #endif /* ENABLE_MANAGEMENT */
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
 /*
  * Initialize and possibly randomize the connection list.
  *
@@ -481,7 +476,7 @@
         int i;
         for (i = l->len - 1; i > 0; --i)
         {
-            const int j = get_random() % (i + 1);
+            const long j = get_random() % (i + 1);
             if (i != j)
             {
                 struct connection_entry *tmp;
@@ -841,7 +836,7 @@
     struct timeval tv;
     if (!gettimeofday(&tv, NULL))
     {
-        const unsigned int seed = (unsigned int)tv.tv_sec ^ tv.tv_usec;
+        const unsigned int seed = (unsigned int)(tv.tv_sec ^ tv.tv_usec);
         srandom(seed);
     }
 
@@ -2849,7 +2844,7 @@
     }
 }
 
-static size_t
+static unsigned int
 get_frame_mtu(struct context *c, const struct options *o)
 {
     size_t mtu;
@@ -2870,10 +2865,14 @@
 
     if (mtu < TUN_MTU_MIN)
     {
-        msg(M_WARN, "TUN MTU value (%zu) must be at least %d", mtu, TUN_MTU_MIN);
+        msg(M_WARN, "TUN MTU value (%zu) must be at least %u", mtu, TUN_MTU_MIN);
         frame_print(&c->c2.frame, M_FATAL, "MTU is too small");
     }
-    return mtu;
+    if (mtu > UINT_MAX)
+    {
+        mtu = UINT_MAX;
+    }
+    return (unsigned int)mtu;
 }
 
 /*
@@ -2899,7 +2898,7 @@
      * space to allow server to push "baby giant" MTU sizes */
     frame->tun_max_mtu = max_int(TUN_MTU_MAX_MIN, frame->tun_max_mtu);
 
-    size_t payload_size = frame->tun_max_mtu;
+    unsigned int payload_size = frame->tun_max_mtu;
 
     /* we need to be also large enough to hold larger control channel packets
      * if configured */
@@ -2918,7 +2917,7 @@
 
     /* the space that is reserved before the payload to add extra headers to it
      * we always reserve the space for the worst case */
-    size_t headroom = 0;
+    unsigned int headroom = 0;
 
     /* includes IV and packet ID */
     headroom += crypto_max_overhead();
@@ -2942,11 +2941,11 @@
     /* the space after the payload, this needs some extra buffer space for
      * encryption so headroom is probably too much but we do not really care
      * the few extra bytes */
-    size_t tailroom = headroom;
+    unsigned int tailroom = headroom;
 
 #ifdef USE_COMP
     msg(D_MTU_DEBUG,
-        "MTU: adding %zu buffer tailroom for compression for %zu "
+        "MTU: adding %u buffer tailroom for compression for %u "
         "bytes of payload",
         COMP_EXTRA_BUFFER(payload_size), payload_size);
     tailroom += COMP_EXTRA_BUFFER(payload_size);
@@ -3288,18 +3287,15 @@
     if (options->renegotiate_seconds_min < 0)
     {
         /* Add 10% jitter to reneg-sec by default (server side only) */
-        int auto_jitter = options->mode != MODE_SERVER
-                              ? 0
-                              : get_random() % max_int(options->renegotiate_seconds / 10, 1);
+        int jitter_max = max_int(options->renegotiate_seconds / 10, 1);
+        int auto_jitter = options->mode != MODE_SERVER ? 0 : (int)(get_random() % jitter_max);
         to.renegotiate_seconds = options->renegotiate_seconds - auto_jitter;
     }
     else
     {
         /* Add user-specified jitter to reneg-sec */
-        to.renegotiate_seconds =
-            options->renegotiate_seconds
-            - (get_random()
-               % max_int(options->renegotiate_seconds - options->renegotiate_seconds_min, 1));
+        int jitter_max = max_int(options->renegotiate_seconds - options->renegotiate_seconds_min, 1);
+        to.renegotiate_seconds = options->renegotiate_seconds - (int)(get_random() % jitter_max);
     }
     to.single_session = options->single_session;
     to.mode = options->mode;
@@ -3491,10 +3487,6 @@
     }
 }
 
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
 /*
  * No encryption or authentication.
  */

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I41b9f7686180f2549bd4984cbbf66059f0ba2b15
Gerrit-Change-Number: 1259
Gerrit-PatchSet: 15
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>

#5 lore raw
In reply to a message not in the archive

Attention is currently required from: cron2, plaisthos.

flichtenheld has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email )

Change subject: init: Fix conversion warnings
......................................................................

Patch Set 15:

(1 comment)

Patchset:

PS1:

Quoted 1 line

I have rechecked my changes. […]

Came up with a compromise?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1259?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I41b9f7686180f2549bd4984cbbf66059f0ba2b15
Gerrit-Change-Number: 1259
Gerrit-PatchSet: 15
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Comment-Date: Fri, 13 Mar 2026 16:39:56 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: cron2 <[email protected]...>
Comment-In-Reply-To: flichtenheld <[email protected]...>